package com.msmall.community.klg.action.html;

import java.util.List;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.klg.model.Article;
import com.msmall.community.klg.service.ArticleManager;
import com.msmall.community.klg.service.KlgManager;

public class KlgAction extends HTMLBaseAction{

	private static final long serialVersionUID = 4339564580715697380L;

	private Long articleid;
	
	private Long topSegmentid;
	
	private Long segid;
	
	private Article article;
	
	private List<CascadeCollection<Segment>> children;
	
	private CascadeCollection<Segment> current;
	
	private ArticleManager articleManager;
	
	private KlgManager klgManager;
	
	
	/**
	 * go to klg index
	 */
	public String execute(){
		initTopSegment();
		CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
		children = cs.getAllDatas().get(topSegmentid).getChildren();
		return SUCCESS;
	}

	/**
	 * 进入知识子模块
	 */
	public String list(){
		initTopSegment();
		return SUCCESS;
	}
	/**
	 * 进入知识子模块,获得此子模块下文章列表
	 */
	public String fetch(){
		try{
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			current = cs.getAllDatas().get(segid);
			page.setPageSize(50);
			page.setAutoCount(false);
			articleManager.getEnabledArticlePaginationBySegment(page, userid, segid);
		}catch(Exception e){
			log.debug(e);
		}
		return SUCCESS;
	}
	
	public void initTopSegment(){
		if(topSegmentid == null || topSegmentid.compareTo(0L) ==0){
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			CascadeCollection<Segment> segment = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_NAV);
			if(segid == null || segid.compareTo(0L)==0){
				topSegmentid = segment.getChildren().get(0).getCurrent().getSegmentid();
				segid = segment.getChildren().get(0).getChildren().get(0).getCurrent().getSegmentid();
			}else{
				CascadeCollection<Segment> current = cs.getAllDatas().get(segid);
				if(current.getFirstParent().getCurrent().getSegmentid().compareTo(ConstantDB.SEGMENT_ID_NAV) == 0){
					topSegmentid = current.getCurrent().getSegmentid();
				}else{
					topSegmentid = current.getFirstParent().getCurrent().getSegmentid();
				}
			}
		}
	}
	public Long getArticleid() {
		return articleid;
	}

	public void setArticleid(Long articleid) {
		this.articleid = articleid;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Long getSegid() {
		return segid;
	}

	public void setSegid(Long segid) {
		this.segid = segid;
	}

	public List<CascadeCollection<Segment>> getChildren() {
		return children;
	}

	public void setChildren(List<CascadeCollection<Segment>> children) {
		this.children = children;
	}

	public void setArticleManager(ArticleManager articleManager) {
		this.articleManager = articleManager;
	}

	public void setKlgManager(KlgManager klgManager) {
		this.klgManager = klgManager;
	}

	public Long getTopSegmentid() {
		return topSegmentid;
	}

	public void setTopSegmentid(Long topSegmentid) {
		this.topSegmentid = topSegmentid;
	}

	public CascadeCollection<Segment> getCurrent() {
		return current;
	}

	public void setCurrent(CascadeCollection<Segment> current) {
		this.current = current;
	}
}
